package com.yimi.policyservice.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yimi.policyservice.common.annotation.Auth;
import com.yimi.policyservice.common.expetion.ApiException;
import com.yimi.policyservice.dao.policy.PolicyAddrMapper;
import com.yimi.policyservice.dto.PageDto;
import com.yimi.policyservice.dto.enums.DataPermissionEnum;
import com.yimi.policyservice.dto.policyaddr.*;
import com.yimi.policyservice.dto.product.PolicyProductDto;
import com.yimi.policyservice.entity.base.policy.PolicyAddr;
import com.yimi.policyservice.repository.PolicyAddressRepository;
import com.yimi.policyservice.service.PolicyAddrService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jack
 */
@RestController
@RequestMapping("addr")
public class PolicyAddressController extends  BaseController {

    @Autowired
    private PolicyAddrService policyAddrService ;

    @Autowired
    private PolicyAddressRepository policyAddressRepository ;

    @Autowired
    private PolicyAddrMapper policyAddrMapper ;
    /**
     * 创建参保地
     * @param bean
     * @return
     */
    @RequestMapping(value = "",method = RequestMethod.POST)
    @Auth
    public PolicyAddr4Get create(@RequestBody Policy4Create bean){
        PolicyAddr addr =  policyAddrService.create(bean,getLoginUser());
        PolicyAddr4Get get = new PolicyAddr4Get();
        BeanUtils.copyProperties(addr,get);
        return  get ;
    }

    /**
     * 删除参保地
     */
    @RequestMapping(value = "/del",method = RequestMethod.POST)
    @Auth
    public void delete(@RequestBody IdsBean bean){
        boolean use = policyAddrService.checkUse(bean.getIds());
        if(!use){
            throw  new ApiException(400,"参保地被引用，不能删除");
        }
        for (String id: bean.getIds()
             ) {
        policyAddrService.delete(id);
        }
    }

    /**
     * 搜索参保地
     * @return
     */
    @RequestMapping(value = "/search",method = RequestMethod.GET)
    @Auth
    public Object search(@RequestParam(defaultValue = "") String key , @RequestParam(defaultValue = "0") int page , @RequestParam(defaultValue = "20") int pageSize){
        PageHelper.startPage(page,pageSize);
        Page<Policy4Search> addrs = policyAddrMapper.search(key,getLoginUser().getId());
        return new PageDto<Policy4Search>(addrs) ;
    }

    /**
     * 更新参保地
     * @param bean
     */
    @RequestMapping(value = "{id}",method = RequestMethod.PUT)
    @ResponseStatus(value = HttpStatus.NO_CONTENT)
    @Auth
    public void update(@RequestBody Policy4GetPut bean){
        policyAddrService.update(getLoginUser(),bean);
    }

    /**
     * 查看参保地
     */
    @RequestMapping(value = "{id}",method = RequestMethod.GET)
    @Auth
    public Policy4GetPut getOne(@PathVariable String id){
        return policyAddrService.getOne(id);
    }

    /**
     * 获取所有参保地
     * @return
     */
    @RequestMapping(value = "all",method = RequestMethod.GET)
    public List<PolicyAddr> getAll(){
        return  policyAddrService.findAll();
    }


    /**
     * 获取所有参保地
     * @return
     */
    @RequestMapping(value = "allPermission",  method = RequestMethod.GET)
    public List<PolicyAddr> getAllPermission(@Param(value = "permissionType") Integer permissionType){
        List<Integer>permissionTypes = new ArrayList<>();
        if(permissionType==null||DataPermissionEnum.ALL.getValue()==permissionType){
            permissionTypes.add(DataPermissionEnum.SOCIALADDR.getValue());
            permissionTypes.add(DataPermissionEnum.ACCFUNDADDR.getValue());
        }else{
            permissionTypes.add(permissionType);
        }
        return  policyAddrService.findAllByPermission(getLoginUser().getId(),permissionTypes);
    }

    /**
     * 查找社保||公积金 参保地
     * @param type
     * @return
     */
    @RequestMapping(value = "role",method = RequestMethod.GET)
    public List<PolicyAddr> findByType(@RequestParam(defaultValue = "0") int type){
        return  policyAddrService.findByType(type);
    }




}
